rm(list=ls())
set.seed(1234)
library(car)
library(plyr)
library(ggplot2)
library(psycho)
library(texreg)

setwd(".../Data/Raw")

modeldata<-read.csv("NZ.csv") 


##create treatment number 

modeldata$Vignettes_DO_treat1 <-recode(modeldata$Vignettes_DO_treat1, " 1 = 1; else=0")
modeldata$Vignettes_DO_treat2 <-recode(modeldata$Vignettes_DO_treat2, " 1 = 2; else=0")
modeldata$Vignettes_DO_treat3 <-recode(modeldata$Vignettes_DO_treat3, " 1 = 3; else=0")
modeldata$Vignettes_DO_treat4 <-recode(modeldata$Vignettes_DO_treat4, " 1 = 4; else=0")
modeldata$Vignettes_DO_treat5 <-recode(modeldata$Vignettes_DO_treat5, " 1 = 5; else=0")
modeldata$Vignettes_DO_treat6 <-recode(modeldata$Vignettes_DO_treat6, " 1 = 6; else=0")

modeldata$treat<-modeldata$Vignettes_DO_treat1 +  modeldata$Vignettes_DO_treat2 + modeldata$Vignettes_DO_treat3 + modeldata$Vignettes_DO_treat4 + modeldata$Vignettes_DO_treat5 + modeldata$Vignettes_DO_treat6

table(modeldata$treat)

modeldata$treat<-recode(modeldata$treat, " 0 = NA")

modeldata <-subset(modeldata, !is.na(modeldata[,144])) #remove NAs

#create substantive legitmacy and procedural legitmacy standardized scores 

modeldata$decide_citizen <-as.character(modeldata$decide_citizen)
modeldata$decide_women <-as.character(modeldata$decide_women)
modeldata$decide_animal <-as.character(modeldata$decide_animal)
modeldata$fair_women <-as.character(modeldata$fair_women)
modeldata$fair_animal <-as.character(modeldata$fair_animal)
modeldata$decisionprocess <-as.character(modeldata$decisionprocess)
modeldata$overturn <-as.character(modeldata$overturn)
modeldata$council_trust <-as.character(modeldata$council_trust)
modeldata$personalcouncil <-as.character(modeldata$personalcouncil)

modeldata[modeldata =="Very unfair"]<-1
modeldata[modeldata =="Unfair"]<-2
modeldata[modeldata =="Somewhat fair"]<-3
modeldata[modeldata =="Fair"]<-3
modeldata[modeldata =="Very fair"]<-4

modeldata[modeldata =="Strongly disagree"]<-1
modeldata[modeldata =="Disagree"]<-2
modeldata[modeldata =="Agree"]<-3
modeldata[modeldata =="Strongly agree"]<-4
modeldata[modeldata =="Strongly Agree"]<-4

modeldata$decide_citizen<-as.numeric(as.character(modeldata$decide_citizen))
modeldata$decide_women <-as.numeric(as.character(modeldata$decide_women))
modeldata$decide_animal <-as.numeric(as.character(modeldata$decide_animal))
modeldata$fair_women <-as.numeric(as.character(modeldata$fair_women))
modeldata$fair_animal <-as.numeric(as.character(modeldata$fair_animal))
modeldata$decisionprocess <-as.numeric(as.character(modeldata$decisionprocess))
modeldata$overturn <-as.numeric(as.character(modeldata$overturn))
modeldata$council_trust <-as.numeric(as.character(modeldata$council_trust))
modeldata$personalcouncil <-as.numeric(as.character(modeldata$personalcouncil))

#reverse code overturn, so higher values represent higher legitimacy views

modeldata$overturnR<-recode(modeldata$overturn, " 1=4; 2=3; 3=2; 4=1")

harass<-subset(modeldata, treat == 1 | treat == 2 | treat == 3 )

##sexual harassment vignettes (treat 1 = AMP, treat 2 = GBP, treat 3 = Q-GBP)

#Create indicies for three treatment groups 

#subset the data to those that pass the manipulation checks
harass<-subset(harass, harass$decide_harass=="Require sexual harassment training") 

#for substantive legitimacy

factor1<-omega(harass[,c(44, 45)], nfactors=1) 
harass$SubLeg<-factor1$scores[,1]
harass$SubLegStand<-standardize(harass$SubLeg)

#for procedural legit

factor1<-omega(harass[,c(49, 51, 145)], nfactors=1) 
harass$ProLeg<-factor1$scores[,1]
harass$ProLegStand<-standardize(harass$ProLeg)


harass <- harass[!is.na(harass$SubLegStand),] #remove NA values from DV
harass <- harass[!is.na(harass$ProLegStand),] #remove NA values from DV

##subset the animal mistreatment vignettes 

animal<-subset(modeldata, treat == 4 | treat == 5 | treat == 6)

#subset the data to those that pass the manipulation checks
animal <-subset(animal, decide_animal.1 == "Require animal mistreatment training")


#Create indicies for three treatment groups 

#for substantive legitimacy
factor1<-omega(animal[,c(44,46)], nfactors=1) #alpha = 0.87
animal $SubLeg<-factor1$scores[,1]
animal $SubLegStand<-standardize(animal $SubLeg)

#for procedural legitimacy 

factor1<-omega(animal[,c(48, 49, 145)], nfactors=1) #alpha = 0.68
animal $ProLeg<-factor1$scores[,1]
animal $ProLegStand<-standardize(animal $ProLeg)

animal <- animal[!is.na(animal $SubLegStand),] #remove NAs from DV

#create and export model dataset 

NZAgg<-rbind(harass[, c(144, 147, 149, 25, 37)], animal[, c(144, 147, 149, 25, 37)]) 
NZAgg $Country <-rep("NZ", nrow(NZAgg))

write.csv(NZAgg, "NZAgg.csv")






